package cn.circle.mobile.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import cn.circle.commom.dao.impl.BaseDaoImpl;
import cn.circle.mobile.service.IUserService;
import cn.circle.system.model.SysUser;
import cn.circle.system.util.CookieUtils;
import cn.circle.system.util.MD5;
import cn.circle.system.util.ReturnInfo;

@Controller
@EnableWebMvc
@RequestMapping("/mobile")
public class MobileUserController extends  BaseDaoImpl<SysUser> {
	
	private static final Log log = LogFactory.getLog(MobileUserController.class);
	
	@Autowired
	private IUserService userService;
	
	@Value("${REDIS_USER_SESSION_KEY}")
	private String REDIS_USER_SESSION_KEY;
	
	/**
	 * 跳转到登录页面
	 */
	@RequestMapping(value = "/goLogin")
	public String goLogin() {
		return "/WEB-INF/pages/mobile/main/login.jsp";
	}
	
	/**
	 * 响应用户登录请求
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value = "/gologin")
	public ReturnInfo gologin(String phone, String userPwd, HttpServletRequest request, HttpServletResponse response) throws Exception {

		 ReturnInfo info = userService.login(new String(phone.getBytes("iso8859-1"),"utf-8"),
				 new String(MD5.md5(userPwd).getBytes("iso8859-1"),"utf-8"), request,  response);		
		return info;
	}
	
	/**
	 * 跳转到注册页面
	 */
	@RequestMapping(value = "/goRegister")
	public String goRegister() {
		return "/WEB-INF/pages/mobile/main/register.jsp";
	}
	/**
	 * 响应用户注册请求
	 */
	@ResponseBody
	@RequestMapping(value = "/goregister")
	public ReturnInfo goRegister(String passWord,SysUser sysUser, ReturnInfo info, HttpServletRequest request) {
		
		System.out.println("注册成功！！！！！");
		passWord=MD5.md5(passWord);
		sysUser.setPassWord(passWord);
		
	if (userService.register(sysUser)) {
			System.out.println("bb1");
			info.setObj(sysUser);
			info.setSuccess(0);
		} else {
			System.out.println("bb2");
			info.setSuccess(1);
			info.setMsg("手机号和密码不能为空!");
		}
		return info;
	}
	
	/*
	 * 根据token获取用户信息
	 */
	@ResponseBody
	@RequestMapping(value="/getUserByToken")
	public ReturnInfo getUserByToken(String token,ReturnInfo info){
		
		String phone =  userService.getUserByToken(token);
		if(phone == null){
			info.setSuccess(1);
			info.setMsg("无此用户");			
		}
		info.setObj(phone);
		return info;
	}
	
	/*
	 * 退出
	 */
	@ResponseBody
	@RequestMapping(value="/loginOut")
	public ReturnInfo loginOut(HttpServletRequest request,HttpServletResponse response, ReturnInfo info){
		String token = CookieUtils.getCookieValue(request, "USER_TOKEN");
		userService.delteByToken(token , request,  response);
		return info;
	}

}
